<?php

class Controller_Meetings extends Controller_Site {

    public function action_index() {
        //felsorolás, linkek az adott találkozóra, és ott a regformra
        $meetings = ORM::factory('meetings')->order_by('date', 'DESC')->find_all()->as_array();
        $this->content->set('meetings', $meetings);
    }

    public function action_show() {
        $meeting_id = $this->request->param('id');
        $meeting = ORM::factory('meetings')->where('id', '=', $meeting_id)->find();
        $downloads = ORM::factory('downloads')->order_by('id')->where('category_id', '=', $meeting->category_id)->find_all();
        $this->content->set('meeting', $meeting);
        $this->content->set('downloads', $downloads);
    }

    public function action_new() {
        $form = View::factory('meetings/form')->set('action', URL::site('meetings/new'));

        $ckeditor = new CKEditor();
        $config = array(
            array('Source', '-',
                'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-',
                'Undo', 'Redo', '-',
                'Find', 'Replace', '-',
                'SelectAll', '-',
                'Maximize', 'ShowBlocks'),
            array('Bold', 'Italic', 'Underline', 'Strike', '-',
                'Subscript', 'Superscript', '-',
                'NumberedList', 'BulletedList', '-',
                'Outdent', 'Indent', 'Blockquote', '-',
                'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-',
                'Link', 'Unlink', 'Anchor', '-',
                'Image', 'Flash', 'Table', 'HorizontalRule', 'SpecialChar'
            ),
            array('Format', 'Font', 'FontSize', '-', 'TextColor', 'BGColor')
        );

        $form->set('ckeditor', $ckeditor)->set('config', $config);

        if ($this->request->method() == 'POST') {
            try {
                $meetings = new Model_Meetings();
                $form->set('meetings', $meetings);
                if (file_exists('/home/localheroes/wiki/data/pages/talalkozo/' . $this->request->post('wiki_slug')))
                    throw new Exception('Létező Wiki kategória!');
                $this->processForm($meetings);
                $this->setFlash('notice', 'Sikeresen felvetted az új eseményt!');
                $this->request->redirect('admin/tasks');
            } catch (Exception $e) {
                $form->set('e', $e);
            }
        }
        $this->content->set('form', $form);
    }

    protected function processForm($meetings) {
        $category = ORM::factory('category', $meetings->category_id);
        $category->name = $this->request->post('name');
        $category->save();

        $meetings->name = $this->request->post('name');
        $meetings->date = $this->request->post('date');
        $meetings->sub_date = $this->request->post('sub_date');
        $meetings->description = $this->request->post('description');
        $meetings->category_id = $category->id;
        if ($this->request->post('wiki_slug')!='') {
            $meetings->wiki_slug = $this->request->post('wiki_slug');
        }
        $meetings->save();

        $path = '/home/localheroes/wiki/data/pages/talalkozo/' . $this->request->post('wiki_slug');
        if (!file_exists($path)) {
            mkdir($path, 0777, TRUE);
        }
        if (!file_exists($path . '/jelentkezes.txt')) {
            $file = fopen($path . '/jelentkezes.txt', 'w');
            fwrite($file, '~~NOCACHE~~
====== Aktuális jelentkezések ======

^ Csapatnév ^ E-mail ^ Telefon ^ Rendszer ^ Játékosok ^ Megjegyzés ^');
            fclose($file);
        }
    }

    public function action_edit() {
        $form = View::factory('meetings/form')->set('action', URL::site('meetings/edit/' . $this->request->param('id')));
        $meetings = new Model_Meetings($this->request->param('id'));

        $ckeditor = new CKEditor();
        $config = array(
            array('Source', '-',
                'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-',
                'Undo', 'Redo', '-',
                'Find', 'Replace', '-',
                'SelectAll', '-',
                'Maximize', 'ShowBlocks'),
            array('Bold', 'Italic', 'Underline', 'Strike', '-',
                'Subscript', 'Superscript', '-',
                'NumberedList', 'BulletedList', '-',
                'Outdent', 'Indent', 'Blockquote', '-',
                'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-',
                'Link', 'Unlink', 'Anchor', '-',
                'Image', 'Flash', 'Table', 'HorizontalRule', 'SpecialChar'
            ),
            array('Format', 'Font', 'FontSize', '-', 'TextColor', 'BGColor')
        );

        $form->set('ckeditor', $ckeditor)->set('config', $config);

        if ($this->request->method() == 'POST') {
            try {
                $this->processForm($meetings);
                $this->setFlash('notice', 'Sikeresen módosítottad az eseményt!');
                $this->request->redirect('admin/tasks');
            } catch (ORM_Validation_Exception $e) {
                $form->set('errors', $e->errors('models'));
            }
        }
        $form->set('meetings', $meetings);
        $this->content->set('form', $form);
    }

    public function action_delete() {
        $meetings = new Model_Meetings($this->request->param('id'));
        $meetings->delete();
        $this->request->redirect('admin/tasks');
    }

    public function action_participate() {
        $active = ORM::factory('meetings')->where('active', '=', 1)->find();
        $participate = ORM::factory('page')->where('name', '=', 'participate_form')->find();
        $this->content->set('active', $active)->set('participate', $participate);
        if ($this->request->method() == 'POST') {
            require_once(Kohana::find_file('../modules', 'phpmailer' . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'class.phpmailer'));
            try {
                $body = '';
                $active = ORM::factory('meetings')->where('active', '=', 1)->find();
                $file = '/home/localheroes/wiki/data/pages/talalkozo/' . $active->wiki_slug . '/jelentkezes.txt';

                $fh = fopen($file, 'a');
                $stringData = "\n";
                fwrite($fh, $stringData);
                $stringData = "| " . ($_POST["teamname"]);
                $body .=$stringData;
                fwrite($fh, $stringData);
                $stringData = " | <" . ($_POST["email"]) . ">";
                $body .=$stringData;
                fwrite($fh, $stringData);
                $stringData = " | " . ($_POST["phone"]);
                $body .=$stringData;
                fwrite($fh, $stringData);
                $stringData = " | " . ($_POST["system"]);
                $body .=$stringData;
                fwrite($fh, $stringData);
                $stringData = " | " . ($_POST["member_1"]);
                $body .=$stringData;
                fwrite($fh, $stringData);
                $stringData = ", " . ($_POST["member_2"]);
                $body .=$stringData;
                fwrite($fh, $stringData);
                $stringData = ", " . ($_POST["member_3"]);
                $body .=$stringData;
                fwrite($fh, $stringData);
                $stringData = ", " . ($_POST["member_4"]);
                $body .=$stringData;
                fwrite($fh, $stringData);
                $stringData = ", " . ($_POST["member_5"]);
                $body .=$stringData;
                fwrite($fh, $stringData);
                $stringData = ", " . ($_POST["member_6"]);
                $body .=$stringData;
                fwrite($fh, $stringData);
                $temp = str_replace(PHP_EOL, '', $_POST["comment"]);
                $stringData = " | " . ($temp) . " | ";
                $body .=$stringData;
                fwrite($fh, $stringData);
                fclose($fh);

                $mailer = new PHPmailer();
                $mailer->IsSMTP();
                $mailer->CharSet = 'UTF-8';
                $mailer->Host = 'localhost';
                $mailer->SetFrom('localheroes@gmail.com', 'Local Heroes jelentkeztető form');
                $mailer->Subject = 'Találkozó jelentkezés';
                $mailer->AddAddress('localheroes@gmail.com', "Local Heroes");
                $mailer->Body = utf8_decode($body);

                $mailer->Send();
                $this->setFlash('notice', 'Sikeresen jelentkeztél!');
                //$this->request->redirect('meetings');
            } catch (ORM_Validation_Exception $e) {
                $form->set('errors', $e->errors('models'));
            }
        }
    }

    public function action_selectactive() {
        if ($this->request->method() == 'POST') {
            $old = ORM::factory('meetings')->where('active', '=', 1)->find();
            $new = ORM::factory('meetings', $this->request->post('active'));
            if ($old->loaded()) {
                $old->active = 0;
                $old->save();
            }
            if ($new->id != NULL) {
                $new->active = 1;
                $new->save();
            }

            if ($this->request->post('active') == 0) {
                $old = ORM::factory('meetings')->where('active', '=', 1)->find();
                if ($old->id != NULL) {
                    $old->active = 0;
                    $old->save();
                }
            }
            $this->setFlash('notice', 'Sikeresen kiválasztás!');
            $this->request->redirect('admin/tasks');
        }
    }

}

?>
